package com.example.WebTask1.Dao;

import com.example.WebTask1.Enity.ClassGrade;
import com.example.WebTask1.Enity.Student;
import com.example.WebTask1.Until.DbUntil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ClassGradeImp implements ClassGradeDao{
    @Override
    public List<ClassGrade> queryClass() {
        List<ClassGrade> list=new ArrayList<>();
        try {
            Connection connection = DbUntil.getConnection();
            String sql="select * from class ";
            PreparedStatement sql_statement = connection.prepareStatement(sql);
            ResultSet resultSet = sql_statement.executeQuery();

            while (resultSet.next()){
                list.add(new ClassGrade(resultSet.getInt(1),
                        resultSet.getString(2),
                        resultSet.getString(3),
                        resultSet.getString(4),
                        resultSet.getString(5),
                        resultSet.getInt(6),
                        resultSet.getInt(7)));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return list;
    }

    @Override
    public Boolean deleteClass(ClassGrade c) {
        Boolean b=false;
        try {
            Connection connection = DbUntil.getConnection();
            String sql="delete from class where cid=?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,c.getCid());
            int i = preparedStatement.executeUpdate();
            if (i==1){
                b=true;
            }else {
                b=false;
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return b;

    }

    @Override
    public Boolean updateClass(ClassGrade c) {
        Boolean b=false;
        try {
            System.out.println("2222222");
            System.out.println(c);
            Connection connection = DbUntil.getConnection();
            String sql="UPDATE class SET className=?, grade=?,chairman=?,slogan=? ,counts=? WHERE cid=?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,c.getClassName());
            preparedStatement.setString(2,c.getGrade());
            preparedStatement.setString(3,c.getChairman());
            preparedStatement.setString(4,c.getSlogan());
            preparedStatement.setInt(5, c.getCount( ));
            preparedStatement.setInt(6,c.getCid());
            int i = preparedStatement.executeUpdate();
            System.out.println("________________________________-");
            System.out.println(i);
            if (i==1){
                b=true;
            }else {
                b=false;
            }


        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return b;
    }

    @Override
    public Boolean insertClass(ClassGrade c) {
        Boolean b=false;
        try {
            Connection connection = DbUntil.getConnection();
            String sql="INSERT into class VALUES(null,?,?,?,?,?,null );";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,c.getClassName());
            preparedStatement.setString(2,c.getGrade());
            preparedStatement.setString(3,c.getChairman());
            preparedStatement.setString(4, c.getSlogan());
            preparedStatement.setInt(5,c.getCount());

            int i = preparedStatement.executeUpdate();
            if (i==1){
                b=true;
            }else {
                b=false;
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return b;
    }
}
